package com.demo.sdfileiodemo;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class SDFileIOActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
        setContentView(R.layout.file_io);
        final EditText edit1 = (EditText) findViewById(R.id.edit1);
        final EditText edit2 = (EditText) findViewById(R.id.edit2);
        Button btn1 = (Button) findViewById(R.id.button1);
        Button btn2 = (Button) findViewById(R.id.button2);

        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String str = edit1.getText().toString();
                write(str);
                edit1.setText("");
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String str = read();
                edit2.setText(str);
            }
        });
    }

    private void write(String str) {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "未检测到SD卡", Toast.LENGTH_LONG).show();
            return;
        }
        FileOutputStream fos = null;
        try {
            File sdcard = Environment.getExternalStorageDirectory();
            fos = new FileOutputStream(sdcard.getCanonicalPath() + "/file.dat");
            fos.write(str.getBytes());
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            try {
                fos.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
    }

    private String read() {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "未检测到SD卡", Toast.LENGTH_LONG).show();
            return null;
        }
        FileInputStream fis = null;
        BufferedReader br = null;
        String result = "";
        try {
            File sdcard = Environment.getExternalStorageDirectory();
//			fis=new FileInputStream(sdcard.getCanonicalPath()+"/file.dat");
            fis = new FileInputStream("/mnt/sdcard/file.dat");
            br = new BufferedReader(new InputStreamReader(fis));
            String line = null;
            while ((line = br.readLine()) != null) {
                result += line;
            }

        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            try {
                br.close();
                fis.close();
            } catch (Exception e2) {
                // TODO: handle exception
            }
        }
        return result;
    }

}